package com.youan.serve.controller;

import com.youan.pangu.base.bean.ElementTree;
import com.youan.pangu.base.exception.BusinessException;
import com.youan.serve.controller.swagger.FunctionControllerSwagger;
import com.youan.serve.service.IFunctionService;
import com.youan.serve.dao.domain.Function;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;




import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto_code
 * @since 2020-03-04
 */
@Controller
@RequestMapping("/function")
public class FunctionController implements FunctionControllerSwagger{
    @Autowired
    private IFunctionService functionService;

    @Override
    @PostMapping("/function")
    public Function addFunction(Function function) throws BusinessException {
        if(functionService.validateFun(function)) {
            functionService.save(function);
        }
        return null;
    }

    @Override
    @PutMapping("/function")
    public Function updateFunction(Function function){
        functionService.updateById(function);
        return function;
    }

    @Override
    @GetMapping("/functions")
    public List<Function> selectAllFunction(){
        return functionService.list();
    }

    @Override
    @DeleteMapping("/function/{funId}")
    public void deleteFunctionByFunId(@PathVariable String funId){
        functionService.removeById(funId);
    }

    @Override
    @GetMapping("/function/{funId}")
    public Function selectFunctionByFunId(@PathVariable String funId){
        return functionService.getById(funId);
    }

    @GetMapping("/getFunctionsElementTree")
    public List<ElementTree> getFunctionsElementTree() {
        return functionService.getFunctionsElementTree();
    }
}

